import { Message } from "@/utils/Message.ts"

export const TopDesignApi = {
  first: 'Ceiling.CeilingStyleFinish',
  defaultData: 'Ceiling.CeilingStyle,DefaultPage',
  deleteById: 'Ceiling.CeilingStyle,Delete'
}

// 发送数据  
const sendData = (cmd: string, data: Record<string, any>) => {
  Message.jsMessage(cmd, JSON.stringify(data))
}

// 订阅数据
const getData = <T = { ceilingStyle: ICeilingStyle }>(cmd: string) => {
  return new Promise<T>((resolve, reject) => {

    Message.addCppMessageCallback(cmd, (data: T) => {
      resolve(data)
    })
  })
}

// 
export const useData = (name: string) => {
  const getDefaultData = <T = { ceilingStyle: ICeilingStyle }>() => {
    return new Promise<T>((resolve, reject) => {

      Message.addCppMessageCallback(TopDesignApi.defaultData, data => {
        resolve(data)
      })
    })
  }
  getDefaultData()
  //告诉后台 页面已经准备好
  sendData(name, {})





  return {
    getDefaultData,
    sendData,
    getData
  }
}   